<?php
/***************************************************************************

 * Copyright (c) 2015 Baidu.com, Inc. All Rights Reserved
 * 
**************************************************************************/

namespace app\controller\wmapi;

use Tinywan\Validate\Validate;


use App\Library\Y;
use app\model\DcdSpecsCans;
use app\model\WaAdminRoles;
use app\model\WaApproveSetting;
use app\model\WaBillCode;
use app\model\WaConfig;
use app\model\WaDepartment;
use app\model\WaSupplier;
use app\model\WaSupplierDomesticLogisticsFees;
use app\model\WaSupplierExamineApprove;
use app\model\WaSupplierInsuranceCompany;
use app\model\WaSupplierRegistrationFee;
use app\model\WaSupplierTransferExportFee;
use App\Service\RedisService;
use Firebase\JWT\BeforeValidException;
use Firebase\JWT\ExpiredException;
use Firebase\JWT\SignatureInvalidException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use plugin\admin\app\common\Util;
use plugin\admin\app\controller\Crud;
use plugin\admin\app\model\User;
use support\Request;
use support\Response;
use Tinywan\Jwt\Exception\JwtCacheTokenException;
use Tinywan\Jwt\JwtToken;
use Workerman\Worker;
use app\model\WaAdmins;
use app\model\WaUsersProvider;
use app\model\WpPosts;
use app\model\WaOrders;
use app\model\WaPurchaseOrdersExamineApprove;
use app\model\WaPurchaseContractExamineApprove;
use app\model\WaSalesOrdersExamineApprove;
use app\model\WaSalesContractExamineApprove;
use app\functions;
use Asan\PHPExcel;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Service;
use app\model\DcdSeries;


/**
 * @file baidu_transapi.php 
 * @author mouyantao(mouyantao@baidu.com)
 * @date 2015/06/23 14:32:18
 * @brief 
 *  
 **/

define("CURL_TIMEOUT",   10);
define("URL",            "http://api.fanyi.baidu.com/api/trans/vip/translate");
//define("APP_ID",         "20250703002396590"); //替换为您的APPID
//define("SEC_KEY",        "VoTQXV7Lre_avIPgxIpt");//替换为您的密钥
define("APP_ID",         "20250516002358920"); //替换为您的APPID
define("SEC_KEY",        "v5rTEwbcXE5HtjCW6H3g");//替换为您的密钥

class BaiduTransapi extends Validate
{
//翻译入口
    public function translate(Request $request)
    {

        $param = $request->all();
        $query = $param['text'];
        $args = array(
            'q' => $query,
            'appid' => APP_ID,
            'salt' => rand(10000,99999),
            'from' => 'zh', // 源语言，例如中文是zh
            'to' => 'en',  // 目标语言，例如英文是en
//            'from' => $from, // 源语言，例如中文是zh
//            'to' => $to, // 目标语言，例如英文是en

        );
        $args['sign'] = self::buildSign($query, APP_ID, $args['salt'], SEC_KEY);
        $ret = self::call(URL, $args);
//        $ret = json_encode($ret, true);
        return IndexController::successed($ret, '销售订单不分页列表');

//        return $ret;
    }

//加密
   public static function buildSign($query, $appID, $salt, $secKey)
    {/*{{{*/
        $str = $appID . $query . $salt . $secKey;
        $ret = md5($str);
        return $ret;
    }/*}}}*/

//发起网络请求
    public static function call($url, $args=null, $method="post", $testflag = 0, $timeout = CURL_TIMEOUT, $headers=array())
    {/*{{{*/
        $ret = false;
        $i = 0;
        while($ret === false)
        {
            if($i > 1)
                break;
            if($i > 0)
            {
                sleep(1);
            }
            $ret = self::callOnce($url, $args, $method, false, $timeout, $headers);
            $i++;
        }
        return $ret;
    }/*}}}*/

    public static function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = CURL_TIMEOUT, $headers=array())
    {/*{{{*/
        $ch = curl_init();
        if($method == "post")
        {
            $data = self::convert($args);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
        }
        else
        {
            $data = self::convert($args);
            if($data)
            {
                if(stripos($url, "?") > 0)
                {
                    $url .= "&$data";
                }
                else
                {
                    $url .= "?$data";
                }
            }
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if(!empty($headers))
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if($withCookie)
        {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
        }
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }/*}}}*/

    public static function convert(&$args)
    {/*{{{*/
        $data = '';
        if (is_array($args))
        {
            foreach ($args as $key=>$val)
            {
                if (is_array($val))
                {
                    foreach ($val as $k=>$v)
                    {
                        $data .= $key.'['.$k.']='.rawurlencode($v).'&';
                    }
                }
                else
                {
                    $data .="$key=".rawurlencode($val)."&";
                }
            }
            return trim($data, "&");
        }
        return $args;
    }/*}}}*/

}

?>
